package recover_func

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

// RequestRecoverPanic 请求panic
func RequestRecoverPanic(ctx *context.Context) {
	if err := recover(); err != nil {
		if err == beego.ErrAbort {
			return
		}
		if !beego.BConfig.RecoverPanic {
			panic(err)
		}
		//sysCtxI := ctx.Input.GetData("ReqContext")
		//sysCtx := sysContext.Background()
		//if sysCtxI != nil {
		//	sysCtx = sysCtxI.(sysContext.Context)
		//}
		if ctx.Output.Status != 0 {
			ctx.ResponseWriter.WriteHeader(ctx.Output.Status)
		} else {
			ctx.ResponseWriter.WriteHeader(200)
		}
		response := map[string]interface{}{
			"code":    6000,
			"message": "服务器开小差",
		}
		jb, _ := json.Marshal(response)
		ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
		_, _ = ctx.ResponseWriter.Write(jb)
	}
}
